package com.cn.jdia.dc.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.cn.jdia.dc.model.Mqconfig;
import com.cn.jdia.dc.model.Mqqueue;
import com.cn.jdia.dc.service.IMqconfigService;
import com.cn.jdia.dc.service.IMqqueueService;
import com.google.gson.Gson;

@Controller
public class QueueMonitorController {

	@Resource
	private IMqconfigService mqconfigService;

	@Resource
	private IMqqueueService mqqueueService;
	
	@RequestMapping(value = "/queuemonitor")
	@ResponseBody
	public ModelAndView queueMonitor() {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("queuemonitor");

		List<Mqconfig> configList = mqconfigService.getMqconfigByContentType("QUEUE");

		List<Map<String, String>> list = new ArrayList<>();
		Map<String, String> map;
		for (Mqconfig config : configList) {
			map = new HashMap<>();
			map.put("sname", config.getServername());
			map.put("ip", config.getIp());
			list.add(map);
		}

		mav.addObject("list", list);
		return mav;
	}

	@RequestMapping(value = "/queueselect", produces = "application/text;charset=utf-8")
	@ResponseBody
	public String queueSelected(String topic) {
		String[] pro = topic.split(" ");
		
		List<Mqqueue> queueList = mqqueueService.getQueueDataByPro(pro[0] ,pro[1]);
		
		List<Map<String, Object>> list = new ArrayList<>();
		Map<String, Object> map;
		
		for(Mqqueue mqqueue:queueList){
			map = new HashMap<>();

			map.put("name", mqqueue.getName());
			map.put("pending", mqqueue.getPending());
			map.put("consumer", mqqueue.getConsumer());
			map.put("enqueue", mqqueue.getEnqueue());
			map.put("dequeue", mqqueue.getDequeue());

			list.add(map);
		}

		Gson gson = new Gson();
		String json = gson.toJson(list);
		return json;

	}
	
}
